package com.iot.ic.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM","yyMMddHHmmss"};

    public static Date parseDate(Object str) {
        try {
            return org.apache.commons.lang3.time.DateUtils.parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * "0 = yyyy-MM-dd",    * 1= yyyy-MM-dd HH:mm:ss", 2= yyyy-MM-dd HH:mm", 3 = "yyyy-MM",
     * "4= yyyy/MM/dd", 5 = yyyy/MM/dd HH:mm:ss", "6= yyyy/MM/dd HH:mm", "7=yyyy/MM",
     * "8=yyyy.MM.dd", "9=yyyy.MM.dd HH:mm:ss", "10=yyyy.MM.dd HH:mm", "11=yyyy.MM"
     *
     * @param date
     * @param i
     * @return
     */
    public static String formatDate(Date date, int i) {
        SimpleDateFormat sf = new SimpleDateFormat(parsePatterns[i]);
        if (date == null) {
            return sf.format(new Date());
        } else {
            return sf.format(date);
        }
    }
    /**
     * "0 = yyyy-MM-dd,     1= yyyy-MM-dd HH:mm:ss, 2= yyyy-MM-dd HH:mm ,
     * 3 = "yyyy-MM",
     * "4= yyyy/MM/dd", 5 = yyyy/MM/dd HH:mm:ss", "6= yyyy/MM/dd HH:mm", "7=yyyy/MM",
     * "8=yyyy.MM.dd", "9=yyyy.MM.dd HH:mm:ss", "10=yyyy.MM.dd HH:mm", "11=yyyy.MM"
     */
    public static String formatNow(int i) {
        return formatDate(null,i);
    }
}
